﻿using ClientAppraise.Controls;
using ClientAppraise.Services;
using ClientAppraise.ViewModels;
using ClientAppraise.ViewModels.Pages;
using ClientAppraise.ViewModels.Pages.Users;
using ClientAppraise.Views.Pages.Users;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Wpf.Ui;
using Wpf.Ui.Controls;

namespace ClientAppraise.Views.Pages
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : IWindow
    {
        public MainWindowViewModel ViewModel
        {
            get;
        }
        public MainWindow(MainWindowViewModel viewModel, IServiceProvider serviceProvider, IZxNavigationService navigationService)
        {

            ViewModel = viewModel;
            ViewModel.MenuItems = ViewModel.GetMenuItemsByAuth(2);//with navigation
            this.DataContext = this;

            viewModel.ApplicationTitle = "111111111";

            InitializeComponent();

            this.Loaded += MainWindow_Loaded;
   
            navigationService.SetNavigationControl(NavigationView);

            NavigationView.SetServiceProvider(serviceProvider);
            var m = this.RootDrawer.Margin;
            m.Top = this.TitleBar.Height;
            this.RootDrawer.Margin = m;

        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // ViewModel.MenuItems = ViewModel.GetMenuItemsByAuth();//no Navigation

            // NavigationView.MenuItems = ViewModel.GetMenuItemsByAuth();// no Navigation
            // ViewModel.MenuItems.Add(new NavigationViewItem("11", SymbolRegular.Grid20, typeof(TestPage)));// no Navigation

            /*
             NavigationView.MenuItems.Add(
                 new NavigationViewItem("11", SymbolRegular.Grid20, typeof(TestPage)) {
                     IsMenuElement=true,
                     Template = NavigationView.ItemTemplate
                });// no Navigation
            */
        }

        private bool _isUserClosedPane;

        private bool _isPaneOpenedOrClosedFromCode;
        private void NavigationView_SelectionChanged(ZxNavigationView sender, RoutedEventArgs args)
        {
            if (sender is not ZxNavigationView navigationView)
            {
                return;
            }
        }
        //private void OnNavigationSelectionChanged(object sender, RoutedEventArgs e)
        //{
        //    if (sender is not ZxNavigationView navigationView)
        //    {
        //        return;
        //    }

        //    //NavigationView.HeaderVisibility =
        //    //    navigationView.SelectedItem?.TargetPageType != typeof(DashboardPage)
        //    //        ? Visibility.Visible
        //    //        : Visibility.Collapsed;
        //}

        private void MainWindow_OnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (_isUserClosedPane)
            {
                return;
            }

            _isPaneOpenedOrClosedFromCode = true;
            NavigationView.IsPaneOpen = !(e.NewSize.Width <= 1200);
            _isPaneOpenedOrClosedFromCode = false;
        }

        private void NavigationView_OnPaneOpened(ZxNavigationView sender, RoutedEventArgs args)
        {
            if (_isPaneOpenedOrClosedFromCode)
            {
                return;
            }

            _isUserClosedPane = false;
        }

        private void NavigationView_OnPaneClosed(ZxNavigationView sender, RoutedEventArgs args)
        {
            if (_isPaneOpenedOrClosedFromCode)
            {
                return;
            }

            _isUserClosedPane = true;
        }

        public SnackbarPresenter GetSnackbarPresenter()
        {
            return SnackbarPresenter;
        }

        public ContentPresenter GetDialogPresenter()
        {
            return RootContentDialog;
        }

        public ContentPresenter GetDrawerPresenter()
        {
            return RootDrawer;
        }
        public ContentPresenter GetTitleRight()
        {
            return TitleRight;
        }

    }
}
